<?php 
/**
 * 前台登录注册控制器
 * 
 */
class LoginController extends CommonController{

	//用户登录
	public function index(){
		if(IS_POST){
			//获得提交表单内容
			$email=Q('post.email');
			$password=md5(Q('post.password'));
			//匹配用户邮箱和密码
			if(!K('User')->where("email='{$email}'")->find()){
				$this->error('用户名不存在');
			}
			if(!K('User')->where("email='{$email}' AND password='{$password}'")->find()){
				$this->error('密码错误');
			}
			//调出数据
			$data=K('User')->where("email='{$email}'")->find();
			//存入session
			$_SESSION['face']=$data['face'];
			$_SESSION['uid']=$data['uid'];
			$_SESSION['username']=$data['username'];
//			if(Q('post.remember')){
//				//增加seeion生存时间1个月(一个月内自动登录)
//				$time = 24 * 3600 * 30;
//				session_set_cookie_params($time); 
//				session_start();
//				
//				$_SESSION['time'] = time();
//			}
			$this->success('登录成功',U('Index/Index/index'));
			
		}		
	    $this->display();
	}	
	
	//退出登录
	public function out(){
	   session(null);
	   $this->success('退出成功',U('Index/Login/index'));
	}
	
	
	
	//用户注册
	public function register(){
		if(IS_POST){
			//获得提交表单内容
			$email = Q('post.email');
			$password = md5(Q('post.password'));
			$username = Q('post.username');
			$sex = Q('post.sex');
			$face = Q('post.face');
			//组合数组
			$data=array(
			   'email' => $email,
			   'password' => $password,
			   'username' => $username,
			   'sex' => $sex,
			   'face' =>$face,
			);
			//往用户表中追加数据
			K('User')->add($data);
			//注册后自动登录
			//调出数据
			$user=K('User')->where("email='{$email}'")->find();	
			//存入session
			$_SESSION['uid']=$user['uid'];
			$_SESSION['username']=$user['username'];
			$_SESSION['face']=$user['face'];		
			$this->success('注册成功',U('Index/Index/index'));			
		}
	    $this->display();
	}
	
}




 ?>